package com.ruoyi.saleReturn.saleReturnStatusMachine.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.sale.domain.SaleGoodsModel;
import com.ruoyi.sale.mapper.SaleGoodsMapper;
import com.ruoyi.saleReturn.domain.SaleReturnModel;
import com.ruoyi.saleReturn.domain.SaleReturnOrder;
import com.ruoyi.saleReturn.mapper.SaleReturnMapper;
import com.ruoyi.saleReturn.saleReturnStatusMachine.SaleReturnService;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.mapper.WarehousingMapper;
import com.ruoyi.warehousing.mapper.WarehousingRecordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 销售退货状态流转 : 回退
 */
@Component
public class SaleReturnRollback implements SaleReturnService {

    /**
     * 工厂的Key
     */
    private static final String FLAG = "0";

    /**
     * 获取工厂的Key
     * @return
     */
    @Override
    public String get() {
        return FLAG;
    }

    /** 销售退货的Mapper */
    @Autowired
    private SaleReturnMapper mapper;

    @Autowired
    private SaleGoodsMapper saleGoodsMapper;
    @Autowired
    private WarehousingMapper warehousingMapper;
    @Autowired
    private WarehousingRecordsMapper warehousingRecordsMapper;
    /**
     * 执行
     * @param order
     * @return
     */
    @Override
    public Integer execute(SaleReturnOrder saleReturnOrder) {
        // 获取当前订单,拿到订单内的产品列表
        SaleReturnOrder saleReturnOrderResult = mapper.selectSaleReturnOrderAndModelById(saleReturnOrder.getSaleReturnId());
        List<SaleReturnModel> models = saleReturnOrderResult.getSaleReturnModels();
        if (models.isEmpty()) {
            throw new RuntimeException("当前订单无产品信息");
        }
        // 返回结果
        Integer result = 0;
        // 增加库存记录
        for (SaleReturnModel model : models) {
            // 获取销货单内的产品列表
            SaleGoodsModel saleGoodsModelResult = saleGoodsMapper.selectSaleGoodsModelByModelId(model.getSaleGoodsModelId());
            // 通过销货单内的产品列表组装库存数据
            Warehousing warehousingParams = new Warehousing();
            warehousingParams.setSelectId(saleGoodsModelResult.getProductId()); // 屎山逻辑
            warehousingParams.setProductId(saleGoodsModelResult.getProductId()); // 产品ID
            warehousingParams.setProductType(saleGoodsModelResult.getProductType()); // 产品类型
            warehousingParams.setBatch(saleGoodsModelResult.getBatch()); // 产品批号
            // 查询库存数据,获取库存ID
            Warehousing warehousingQueryResult = warehousingMapper.selectWarehousing(warehousingParams);
            // 组装入库记录
            WarehousingRecords warehousingRecordsData = new WarehousingRecords();
            warehousingRecordsData.setWarehousingId(warehousingQueryResult.getWarehousingId()); // 库存ID
            warehousingRecordsData.setSupplierId(warehousingQueryResult.getDeptId()); // 部门ID 屎山
            warehousingRecordsData.setInventoryNum(-model.getReturnNum()); // 操作数量
            warehousingRecordsData.setInventoryStatus("0"); // 入库类型 屎山
            warehousingRecordsData.setCreateTime(new Date()); // 入库时间
            warehousingRecordsData.setCreateBy(SecurityUtils.getUsername()); // 入库操作人
            warehousingRecordsData.setAllnum(-model.getAllNum()); // 操作数量 屎山
            // 新增入库记录
            warehousingRecordsMapper.insertWarehousingRecords(warehousingRecordsData);

            // 修改实时库存批号库存
            Warehousing warehousingData = new Warehousing();
            warehousingData.setWarehousingId(warehousingQueryResult.getWarehousingId()); // 库存Id
            warehousingData.setAmount(warehousingQueryResult.getAmount() - (long) model.getAllNum()); // 库存数量
            result =  warehousingMapper.updateWarehousing(warehousingData);
        }
        return result;
    }
}
